package cn.t235.service;

import cn.t235.dao.UserDao;
import cn.t235.dao.UserDaoJdbcImpl;
import cn.t235.dao.UserDaoMybatisImpl;
import cn.t235.factory.UserDaoFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 天天向上 （john.yi@qq.com）
 * @date 2020/10/28.
 */
@Service
public class UserService {
//    private UserDao userDao = new UserDaoMybatisImpl();
//    private UserDao userDao = UserDaoFactory.createUserDao();
    // userService 依赖了userDao
//    @Autowired  // 默认按类型注入
//    @Qualifier("userDaoJdbcImpl")
    @Resource(name = "userDaoJdbcImpl") // 默认按名字注入
    private UserDao userDao;

    private String[] arr;
    private List<String> list;
    private Set<String> set;
    private Map<String, String> map;
    private Properties properties;

    public UserService() {
    }

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public String[] getArr() {
        return arr;
    }

    public void setArr(String[] arr) {
        this.arr = arr;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public int add(String username) {
        int n = new Random().nextInt(2);
        if(n == 1) {
            throw new NullPointerException("发生了空指针异常:" + n);
        }
        return userDao.add(username);

    }
}
